﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using SharpSqlAnalyzer.pp.logger;
using SharpSqlAnalyzer.pp.para;

namespace SharpSqlAnalyzer.pp.utils
{
	// Token: 0x020001F5 RID: 501
	public class SourceTokenOperator
	{
		// Token: 0x0600117B RID: 4475 RVA: 0x00314890 File Offset: 0x00312A90
		public static int removeWhitespaceBackward(GFmtOpt opt, TSourceTokenList list, int end)
		{
			if (end == 0)
			{
				return 0;
			}
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("removeWhitespaceAndReturnFromEnd:\ntargetToken:").Append(list.get(end).astext).Append(" newToken:").Append(Regex.Replace(list.get(end).astext, "\\n", "\\\\n")).Append("(type:").Append(list.get(end).tokentype).Append(")");
			int num = 0;
			int num2 = end - 1;
			TSourceToken tsourceToken = list.get(num2);
			while (tsourceToken.tokentype == ETokenType.ttwhitespace)
			{
				if (tsourceToken.tokentype == ETokenType.ttwhitespace)
				{
					num++;
				}
				if (opt.operateSourceToken)
				{
					tsourceToken.ReplaceToken = SourceTokenConstant.EMPTY;
				}
				num2--;
				if (num2 < 0)
				{
					break;
				}
				tsourceToken = tsourceToken.container.get(num2);
			}
			if (tsourceToken.tokentype == ETokenType.ttreturn)
			{
				StringBuilder stringBuilder2 = new StringBuilder();
				for (int i = 0; i < tsourceToken.astext.Length; i++)
				{
					char c = tsourceToken.astext[i];
					if (c != ' ')
					{
						stringBuilder2.Append(c);
					}
				}
				tsourceToken.astext = stringBuilder2.ToString();
			}
			stringBuilder.Append("\nDeleteWhiteSpace:").Append(num);
			PPLogger.info(2, stringBuilder.ToString(), null);
			return num2;
		}

		// Token: 0x0600117C RID: 4476 RVA: 0x003149E0 File Offset: 0x00312BE0
		public static int removeWhitespaceAndReturnFromEnd(GFmtOpt opt, TSourceTokenList list, int end)
		{
			if (end == 0)
			{
				return 0;
			}
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("removeWhitespaceAndReturnFromEnd:\ntargetToken:").Append(list.get(end).astext).Append(" newToken:").Append(Regex.Replace(list.get(end).astext, "\\n", "\\\\n")).Append("(type:").Append(list.get(end).tokentype).Append(")");
			int num = 0;
			int num2 = 0;
			int num3 = end - 1;
			TSourceToken tsourceToken = list.get(num3);
			while (tsourceToken.tokentype == ETokenType.ttwhitespace || tsourceToken.tokentype == ETokenType.ttreturn)
			{
				if (tsourceToken.tokentype == ETokenType.ttwhitespace)
				{
					num++;
				}
				if (tsourceToken.tokentype == ETokenType.ttreturn)
				{
					num2++;
				}
				if (opt.operateSourceToken)
				{
					tsourceToken.ReplaceToken = SourceTokenConstant.EMPTY;
				}
				num3--;
				if (num3 < 0)
				{
					break;
				}
				tsourceToken = tsourceToken.container.get(num3);
			}
			stringBuilder.Append("\nDeleteWhiteSpace:").Append(num).Append(", DeleteReturn:").Append(num2);
			PPLogger.info(2, stringBuilder.ToString(), null);
			return num3;
		}

		// Token: 0x0600117D RID: 4477 RVA: 0x00314B07 File Offset: 0x00312D07
		public static int removeWhitespaceAndReturnFromEnd(GFmtOpt opt, TSourceToken token)
		{
			if (token == null)
			{
				return -1;
			}
			return SourceTokenOperator.removeWhitespaceAndReturnFromEnd(opt, token.container, token.posinlist);
		}

		// Token: 0x0600117E RID: 4478 RVA: 0x00314B20 File Offset: 0x00312D20
		public static void removeWhitespaceAndReturnFormBeforeAndAfter(GFmtOpt opt, TSourceToken token)
		{
			if (token == null)
			{
				return;
			}
			SourceTokenOperator.removeWhitespaceAndReturnFromEnd(opt, token);
			SourceTokenOperator.removeWhitespaceAndReturnFromStart(opt, token.container, token.posinlist + 1);
		}

		// Token: 0x0600117F RID: 4479 RVA: 0x00314B44 File Offset: 0x00312D44
		public static void removeWhitespaceAndReturn(GFmtOpt opt, TSourceTokenList list, int start, int end)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("removeWhitespaceAndReturn:\ntargetToken:").Append(list.get(end).astext).Append(" newToken:").Append(Regex.Replace(list.get(end).astext, "\\n", "\\\\n")).Append("(type:").Append(list.get(end).tokentype).Append(")");
			int num = 0;
			int num2 = 0;
			for (int i = start; i < end; i++)
			{
				if (i < 0 || i > list.size())
				{
					return;
				}
				TSourceToken tsourceToken = list.get(i);
				if (tsourceToken.tokentype == ETokenType.ttwhitespace || tsourceToken.tokentype == ETokenType.ttreturn)
				{
					if (tsourceToken.tokentype == ETokenType.ttwhitespace)
					{
						num++;
					}
					if (tsourceToken.tokentype == ETokenType.ttreturn)
					{
						num2++;
					}
					if (opt.operateSourceToken)
					{
						tsourceToken.ReplaceToken = SourceTokenConstant.EMPTY;
					}
				}
			}
			stringBuilder.Append("\nDeleteWhiteSpace:").Append(num).Append(", DeleteReturn:").Append(num2);
			PPLogger.info(2, stringBuilder.ToString(), null);
		}

		// Token: 0x06001180 RID: 4480 RVA: 0x00314C64 File Offset: 0x00312E64
		public static int removeWhitespaceAndReturnFromStart(GFmtOpt opt, TSourceTokenList list, int startPos)
		{
			StringBuilder stringBuilder = new StringBuilder();
			if (list.size() <= startPos)
			{
				return list.size() - 1;
			}
			stringBuilder.Append("removeWhitespaceAndReturnFromStart:\ntargetToken:").Append(list.get(startPos).astext).Append(" newToken:").Append(Regex.Replace(list.get(startPos).astext, "\\n", "\\\\n")).Append("(type:").Append(list.get(startPos).tokentype).Append(")");
			int num = 0;
			int num2 = 0;
			int num3 = startPos;
			TSourceToken tsourceToken = list.get(num3);
			while (tsourceToken.tokentype == ETokenType.ttwhitespace || tsourceToken.tokentype == ETokenType.ttreturn)
			{
				if (tsourceToken.tokentype == ETokenType.ttwhitespace)
				{
					num++;
				}
				if (tsourceToken.tokentype == ETokenType.ttreturn)
				{
					num2++;
				}
				if (opt.operateSourceToken)
				{
					tsourceToken.ReplaceToken = SourceTokenConstant.EMPTY;
				}
				num3++;
				if (num3 >= list.size())
				{
					break;
				}
				tsourceToken = tsourceToken.container.get(num3);
			}
			stringBuilder.Append("\nDeleteWhiteSpace:").Append(num).Append(", DeleteReturn:").Append(num2);
			PPLogger.info(2, stringBuilder.ToString(), null);
			return num3;
		}

		// Token: 0x06001181 RID: 4481 RVA: 0x00314D9C File Offset: 0x00312F9C
		public static int textLengthVT(TSourceTokenList list, int start, int end)
		{
			int num = 0;
			for (int i = start; i < end; i++)
			{
				TSourceToken tsourceToken = list.get(i);
				if (tsourceToken.TokensBefore.size() > 0)
				{
					num += SourceTokenOperator.textLengthVT(tsourceToken.TokensBefore, 0, tsourceToken.TokensBefore.size());
				}
				if (tsourceToken.ReplaceToken != null)
				{
					num += ((tsourceToken.ReplaceToken.astext != null) ? tsourceToken.ReplaceToken.astext.Length : 0);
				}
				else
				{
					num += ((tsourceToken.astext != null) ? tsourceToken.astext.Length : 0);
				}
				if (tsourceToken.TokensAfter.size() > 0)
				{
					num += SourceTokenOperator.textLengthVT(tsourceToken.TokensAfter, 0, tsourceToken.TokensAfter.size());
				}
			}
			return num;
		}

		// Token: 0x06001182 RID: 4482 RVA: 0x00314E5C File Offset: 0x0031305C
		public static int curColumnNumberVT(TSourceTokenList list, int pos)
		{
			int num = 0;
			int i = pos;
			while (i >= 0)
			{
				TSourceToken tsourceToken = list.get(i);
				if (i != pos && tsourceToken.TokensAfter.size() > 0)
				{
					for (int j = tsourceToken.TokensAfter.size() - 1; j >= 0; j--)
					{
						TSourceToken tsourceToken2 = tsourceToken.TokensAfter.get(j);
						if (tsourceToken2.tokentype == ETokenType.ttreturn)
						{
							return num;
						}
						num += ((tsourceToken2.astext != null) ? tsourceToken2.astext.Length : 0);
					}
				}
				if (i == pos)
				{
					goto IL_18A;
				}
				if (tsourceToken.ReplaceToken != null)
				{
					if (tsourceToken.ReplaceToken.tokentype != ETokenType.ttreturn)
					{
						num += ((tsourceToken.ReplaceToken.astext != null) ? tsourceToken.ReplaceToken.astext.Length : 0);
						goto IL_18A;
					}
					return num;
				}
				else
				{
					if (tsourceToken.tokentype == ETokenType.ttreturn)
					{
						return num;
					}
					if (tsourceToken.tokentype == ETokenType.ttsimplecomment)
					{
						if (tsourceToken.nextSolidToken() != null && tsourceToken.nextSolidToken().astext != null && tsourceToken.nextSolidToken().astext.Trim().Length == 0)
						{
							num -= tsourceToken.nextSolidToken().astext.Length;
						}
					}
					else
					{
						if (!SourceTokenOperator.isOracleHint(tsourceToken))
						{
							num += ((tsourceToken.astext != null) ? tsourceToken.astext.Length : 0);
							goto IL_18A;
						}
						if (SourceTokenOperator.getNextToken(tsourceToken) != null && SourceTokenOperator.getNextToken(tsourceToken).astext != null && SourceTokenOperator.getNextToken(tsourceToken).astext.Trim().Length == 0)
						{
							num -= SourceTokenOperator.getNextToken(tsourceToken).astext.Length;
						}
					}
				}
				IL_1EC:
				i--;
				continue;
				IL_18A:
				if (tsourceToken.TokensBefore.size() > 0)
				{
					for (int k = tsourceToken.TokensBefore.size() - 1; k >= 0; k--)
					{
						TSourceToken tsourceToken3 = tsourceToken.TokensBefore.get(k);
						if (tsourceToken3.tokentype == ETokenType.ttreturn)
						{
							return num;
						}
						num += ((tsourceToken3.astext != null) ? tsourceToken3.astext.Length : 0);
					}
					goto IL_1EC;
				}
				goto IL_1EC;
			}
			return num;
		}

		// Token: 0x06001183 RID: 4483 RVA: 0x00315064 File Offset: 0x00313264
		private static TSourceToken getNextToken(TSourceToken token)
		{
			TSourceTokenList container = token.container;
			if (token.posinlist + 1 >= container.Count)
			{
				return null;
			}
			return container[token.posinlist + 1];
		}

		// Token: 0x06001184 RID: 4484 RVA: 0x00315098 File Offset: 0x00313298
		private static bool isOracleHint(TSourceToken token)
		{
			return token != null && token.astext != null && token.astext.StartsWith("/*+");
		}

		// Token: 0x06001185 RID: 4485 RVA: 0x003150B7 File Offset: 0x003132B7
		public static int curColumnNumberVT(TSourceToken token)
		{
			return SourceTokenOperator.curColumnNumberVT(token.container, token.posinlist);
		}

		// Token: 0x06001186 RID: 4486 RVA: 0x003150CC File Offset: 0x003132CC
		public static int curIndentLenVT(TSourceTokenList list, int pos)
		{
			int num = 0;
			for (int i = pos; i >= 0; i--)
			{
				TSourceToken tsourceToken = list.get(i);
				if (i != pos && tsourceToken.TokensAfter.size() > 0)
				{
					for (int j = tsourceToken.TokensAfter.size() - 1; j >= 0; j--)
					{
						TSourceToken tsourceToken2 = tsourceToken.TokensAfter.get(j);
						if (tsourceToken2.tokentype == ETokenType.ttreturn)
						{
							return num;
						}
						if (tsourceToken2.tokentype == ETokenType.ttwhitespace)
						{
							num += ((tsourceToken2.astext != null) ? tsourceToken2.astext.Length : 0);
						}
						else
						{
							num = 0;
						}
					}
				}
				if (i != pos)
				{
					if (tsourceToken.ReplaceToken != null)
					{
						if (tsourceToken.ReplaceToken.tokentype == ETokenType.ttreturn)
						{
							return num;
						}
						if (tsourceToken.ReplaceToken.tokentype == ETokenType.ttwhitespace)
						{
							num += ((tsourceToken.ReplaceToken.astext != null) ? tsourceToken.ReplaceToken.astext.Length : 0);
						}
						else
						{
							num = 0;
						}
					}
					else
					{
						if (tsourceToken.tokentype == ETokenType.ttreturn)
						{
							return num;
						}
						if (tsourceToken.tokentype == ETokenType.ttwhitespace)
						{
							num += ((tsourceToken.astext != null) ? tsourceToken.astext.Length : 0);
						}
						else
						{
							num = 0;
						}
					}
				}
				if (tsourceToken.TokensBefore.size() > 0)
				{
					for (int k = tsourceToken.TokensBefore.size() - 1; k >= 0; k--)
					{
						TSourceToken tsourceToken3 = tsourceToken.TokensBefore.get(k);
						if (tsourceToken3.tokentype == ETokenType.ttreturn)
						{
							return num;
						}
						if (tsourceToken3.tokentype == ETokenType.ttwhitespace)
						{
							num += ((tsourceToken3.astext != null) ? tsourceToken3.astext.Length : 0);
						}
						else
						{
							num = 0;
						}
					}
				}
			}
			return num;
		}

		// Token: 0x06001187 RID: 4487 RVA: 0x00315258 File Offset: 0x00313458
		public static int curIndentLenVT(TSourceToken token)
		{
			return SourceTokenOperator.curIndentLenVT(token.container, token.posinlist);
		}

		// Token: 0x06001188 RID: 4488 RVA: 0x0031526B File Offset: 0x0031346B
		public static TSourceToken createWhitespaceSourceToken(int length)
		{
			return WhitespaceSourceTokenHelper.createWhitespaceSourceToken(length);
		}

		// Token: 0x06001189 RID: 4489 RVA: 0x00315273 File Offset: 0x00313473
		static SourceTokenOperator()
		{
			SourceTokenOperator.noFormatFlag.tokentype = ETokenType.ttwhitespace;
		}

		// Token: 0x0600118A RID: 4490 RVA: 0x0031528F File Offset: 0x0031348F
		public static TSourceToken createNoFormatFlagToken()
		{
			return SourceTokenOperator.noFormatFlag;
		}

		// Token: 0x0600118B RID: 4491 RVA: 0x00315296 File Offset: 0x00313496
		public static TSourceToken createReturnSourceToken()
		{
			return new TSourceToken("\n")
			{
				tokentype = ETokenType.ttreturn
			};
		}

		// Token: 0x0600118C RID: 4492 RVA: 0x003152AC File Offset: 0x003134AC
		public static void combineWhitespace(TSourceTokenList list, int startPos, int endPos)
		{
			bool flag = false;
			for (int i = startPos; i < endPos; i++)
			{
				if (i >= 0 && i < list.size())
				{
					TSourceToken tsourceToken = list.get(i);
					if (tsourceToken.tokentype == ETokenType.ttwhitespace)
					{
						tsourceToken.ReplaceToken = SourceTokenConstant.EMPTY;
						flag = true;
					}
				}
			}
			if (flag)
			{
				list.get(endPos).TokensBefore.add(SourceTokenOperator.createWhitespaceSourceToken(1));
			}
		}

		// Token: 0x0600118D RID: 4493 RVA: 0x0031530C File Offset: 0x0031350C
		public static void addBefore(GFmtOpt opt, TSourceToken targetToken, TSourceToken needAddToken)
		{
			if (needAddToken == null)
			{
				return;
			}
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("AddBefore:\ntargetToken:").Append(targetToken.astext).Append("(pos:").Append(targetToken.posinlist).Append(")").Append(" newToken:").Append(Regex.Replace(needAddToken.astext, "\\n", "\\\\n")).Append("(type:").Append(needAddToken.tokentype).Append(")");
			if (targetToken.TokensBefore.size() > 0)
			{
				stringBuilder.Append("\noldTokenBefore:");
				for (int i = 0; i < targetToken.TokensBefore.size(); i++)
				{
					TSourceToken tsourceToken = targetToken.TokensBefore.get(i);
					stringBuilder.Append("[").Append((tsourceToken.astext != null) ? Regex.Replace(tsourceToken.astext, "\\n", "\\\\n") : "").Append("] ").Append("(type:").Append(tsourceToken.tokentype).Append(")");
				}
			}
			PPLogger.info(2, stringBuilder.ToString(), null);
			if (opt.operateSourceToken)
			{
				targetToken.TokensBefore.add(needAddToken);
			}
		}

		// Token: 0x0600118E RID: 4494 RVA: 0x00315468 File Offset: 0x00313668
		public static void addAfter(GFmtOpt opt, TSourceToken targetToken, TSourceToken needAddToken)
		{
			if (needAddToken == null)
			{
				return;
			}
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("AddAfter:\ntargetToken:").Append(targetToken.astext).Append("(pos:").Append(targetToken.posinlist).Append(")").Append(" newToken:").Append((needAddToken.astext != null) ? Regex.Replace(needAddToken.astext, "\\n", "\\\\n") : "").Append("(type:").Append(needAddToken.tokentype).Append(")");
			if (targetToken.TokensAfter.size() > 0)
			{
				stringBuilder.Append("\noldTokenAfter:");
				for (int i = 0; i < targetToken.TokensAfter.size(); i++)
				{
					TSourceToken tsourceToken = targetToken.TokensAfter.get(i);
					stringBuilder.Append("[").Append((tsourceToken.astext != null) ? Regex.Replace(tsourceToken.astext, "\\n", "\\\\n") : "").Append("] ").Append("(type:").Append(tsourceToken.tokentype).Append(")");
				}
			}
			PPLogger.info(2, stringBuilder.ToString(), null);
			if (opt.operateSourceToken)
			{
				targetToken.TokensAfter.add(needAddToken);
			}
		}

		// Token: 0x0600118F RID: 4495 RVA: 0x003155D4 File Offset: 0x003137D4
		public static void removeAllBeforeTokenVT(GFmtOpt opt, TSourceToken targetToken)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("RemoveAllBeforeTokenVT:");
			while (targetToken.TokensBefore.size() > 0)
			{
				TSourceToken tsourceToken = targetToken.TokensBefore.get(0);
				stringBuilder.Append("\nremoveToken:").Append("[").Append((tsourceToken.astext != null) ? Regex.Replace(tsourceToken.astext, "\\n", "\\\\n") : "").Append("] ").Append("(type:").Append(tsourceToken.tokentype).Append(")");
				if (opt.operateSourceToken)
				{
					targetToken.TokensBefore.remove(0);
				}
			}
			PPLogger.info(2, stringBuilder.ToString(), null);
		}

		// Token: 0x06001190 RID: 4496 RVA: 0x003156A4 File Offset: 0x003138A4
		public static void removeThisToken(TSourceToken token)
		{
			token.ReplaceToken = SourceTokenOperator.createWhitespaceSourceToken(1);
		}

		// Token: 0x040013C9 RID: 5065
		private static TSourceToken noFormatFlag = new TSourceToken("");
	}
}
